var inputs = document.getElementsByTagName('input');

for(var i = 0; i < inputs.length; i++) {
	inputs[i].onblur = inputBlur;

}

function inputBlur() {
	var name = this.name;
	var value = this.value;
	var tips = this.placeholder;

	var div = this.parentNode.nextElementSibling.firstChild;
	value = value.trim();

	if(value == null || value == '') {
		div.innerHTML = '不能为空';
		div.className = 'error';
	} else {
		var reg = getReg(name);
		if(value.match(reg)) {
			div.innerHTML = '输入正确';
			div.className = 'success';

		} else {
			div.innerHTML = tips;
			div.className = 'error';
		}
	}
}

function getReg(name) {
	var reg = '';
	switch(name) {
		case 'username':
			reg = /^[a-zA-Z]{4,12}$/;
			break;
		case 'pwd':
			reg = /^\w{6-20}$/;
			break;
		case 'repwd':
			var pwd = document.getElementsByTagName('input')[1].value;
			reg = new RegExp('^' + pwd + '$');
			break;
		case 'tel':
			reg = /^1[345678]\d{9}$/;
			break;
		case 'email':
			//reg=/(\w(\-|\.)*)+@+(\w+(\-)?)+(\.\w{2,})$/;
			reg = /^\w+@+\w+\.+\w{2,}$/;
			break;
		default:
			break;
	}
	return reg;
}